package com.alliky.core.net.interceptor;

import android.content.Context;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Author: wxianing
 * @CreateDate: 2022/4/7 22:02
 * @Description: java类作用描述
 */
public class CacheInterceptor implements Interceptor {

    private Context context;

    public CacheInterceptor(Context context) {
        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
//        if (NetworkUtil.isNetworkAvailable(context)) {
        Response response = chain.proceed(request);
        // read from cache for 60 s
        int maxAge = 60;
        return response.newBuilder()
                .removeHeader("Pragma")
                .removeHeader("Cache-Control")
                .header("Cache-Control", "public, max-age=" + maxAge)
                .build();
//        } else {
//            //读取缓存信息
//            request = request.newBuilder()
//                    .cacheControl(CacheControl.FORCE_CACHE)
//                    .build();
//            Response response = chain.proceed(request);
//            //set cache times is 3 days
//            int maxStale = 60 * 60 * 24 * 3;
//            return response.newBuilder()
//                    .removeHeader("Pragma")
//                    .removeHeader("Cache-Control")
//                    .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
//                    .build();
//        }
    }
}
